package com.uk.ui.manaxhim;

import com.uk.data.containers.UserContainer;
import com.uk.data.ejbs.IFaturaBean;
import com.uk.ui.CHorizontalSplitPanel;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.VerticalSplitPanel;


public class ManaxhimPanel extends CHorizontalSplitPanel implements ItemClickListener,ValueChangeListener{

	
	private UserContainer userContainer;
	
	private IFaturaBean faturaBean;
	private final ManaxhimNavigationTree manaxhimNavigationTree;
	private UserManagementListView userManagementListView;
	private UserForm userForm;
	private UserList userList;
	
	private ParamsListView paramsListView;
	private ParamsForm paramsForm;
	private ParamsList paramsList;
	
	
	public ManaxhimPanel(IFaturaBean faturaBean) {
		super();
		this.manaxhimNavigationTree = new ManaxhimNavigationTree(this);
		this.faturaBean = faturaBean;
		this.setSizeFull();
		this.setSplitPosition(170, VerticalSplitPanel.UNITS_PIXELS);
		this.setFirstComponent(this.manaxhimNavigationTree);
		this.setSecondComponent(this.getUserManagementListView());
	}


	
	private ParamsListView getParamsListView(){
		if (this.paramsListView == null){
			this.paramsList = new ParamsList(this);
			this.paramsForm = new ParamsForm(this);
			this.paramsListView = new ParamsListView(paramsList, paramsForm);
		}
		else{
			this.paramsList = new ParamsList(this);
			this.paramsListView.setParamsList(this.paramsList);
		}
		return this.paramsListView;
	}
	
	
	private UserManagementListView getUserManagementListView(){
		if (this.userManagementListView == null){
			this.userList = new UserList(this);
			this.userForm = new UserForm(this);
			this.userManagementListView = new UserManagementListView(userList, userForm);
			
		}
		else {
			this.userList = new UserList(this);
			this.userManagementListView.setUserList(userList);
			
		}
		return this.userManagementListView;
		
	}
	@Override
	public void itemClick(ItemClickEvent event) {
		System.out.println("ManaxhimPanel.itemClick()>>>>>>>>>>>>>>>>");
		System.out.println(event.getItemId());
		if (event.getComponent() == this.manaxhimNavigationTree){
			
			Object itemId = event.getItemId();
			
			if (itemId != null){
				if (ManaxhimNavigationTree.USERS.equals(itemId)){
					this.setSecondComponent(this.getUserManagementListView());
				}
				else if (ManaxhimNavigationTree.ADD_USER.equals(itemId)){
					this.setSecondComponent(this.getUserManagementListView());
					this.userForm.addUser();
				}
				//else if(ManaxhimNavigationTree.CHANGE_PARAMS.equals(itemId)){
				//	this.setSecondComponent(this.getParamsListView());
				//}
			}
				
				
		}
		
	}


	public  UserContainer getUserContainer() {
		if (this.userContainer == null) this.userContainer = UserContainer.createFromEjb(faturaBean);
		return userContainer;
	}

	
	public IFaturaBean getFaturaBean(){
		return this.faturaBean;
	}



	
	@Override
	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
		
		if (property == userList){
			Item item = userList.getItem(userList.getValue());
			if (item != userForm.getItemDataSource()){
				userForm.setItemDataSource(item);
			}
		}
		else if (property == paramsList){
			Item item = paramsList.getItem(paramsList.getValue());
			if (item != paramsForm.getItemDataSource()){
				paramsForm.setItemDataSource(item);
			}
		}
	}
}
